//
//  NewFeatureViewController.swift
//  sinaweibowj
//
//  Created by Macintosh on 15/12/17.
//  Copyright © 2015年 mactoinsh. All rights reserved.
//

import UIKit
import SnapKit
private let imageCount = 4
private let reuseIdentifier = "Cell"
class NewFeatureViewController: UICollectionViewController {
    
    init (){
        //调用指定的构造方法实例化对象
        let layout  = UICollectionViewFlowLayout()
        //修改布局
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置上下左右间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        //super.init 是构造的最后一步
        super.init(collectionViewLayout: layout)
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        //取消弹簧效果
        collectionView?.bounces = false
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.orangeColor() : UIColor.purpleColor()
        cell.index = indexPath.item
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //print(indexPath.item)
        //获取准确的cell
        let cell = collectionView.visibleCells().last! as! NewFeatureCell
        let index = collectionView.indexPathForCell(cell)
        //print(index?.item)
        if index?.item == imageCount - 1 {
        cell.showAnimation()
        }
    }
}



//在一个文件中可以同时建多个类
class NewFeatureCell :UICollectionViewCell{

    var index :Int = 0{
    //和oc重写set方法一样，
        didSet{
        
            iconView.image =  UIImage(named: "new_feature_\(index+1)")
        
            //startBtn.hidden = index != 3
           //showAnimation()
            startBtn.hidden = true
        }
    
    }
    
    private func showAnimation(){
    //动画开始之前，修改frame
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        startBtn.hidden = false
        
        UIView.animateWithDuration(0.5, delay: 0, options: [], animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    }
    
    
    //重写init
    override init(frame: CGRect) {
        super.init(frame: frame)
        print(frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    private func setUpUI (){
    
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
    //使用SnapKit添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            //make标识需要添加约束的对象
            make.edges.equalTo(contentView.snp_edges)
        }
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        
        //添加按钮的点击事件
        startBtn.addTarget(self , action: "startBtnClick", forControlEvents: .TouchUpInside)
    }
    
    @objc private  func startBtnClick(){
    print("__FUNCTION__")
    //动画结束的时候 发出 页面切换的通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
    }
    
    //懒加载所有的子视图
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))

    //懒加载开始按钮
    private lazy var startBtn:UIButton = {
    
    let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        btn.tintColor = UIColor.whiteColor()
        btn.sizeToFit()
        
        return btn
    }()
    

}


